package com.hele.sts.pub;

import com.sun.media.jai.codec.*;

import java.io.*;
import java.awt.*;
import java.awt.image.*;

import javax.media.jai.*;

import java.awt.image.renderable.*;
import java.awt.geom.*;

import javax.media.jai.JAI;

import java.awt.print.Printable;
import java.awt.print.PrinterJob;
import java.awt.print.PageFormat;

import javax.print.*;
import javax.print.event.*;
import javax.print.attribute.*;
import javax.print.attribute.standard.*;

import java.awt.print.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;

import javax.swing.*;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class TiffPrinter implements java.awt.print.Printable {
	private javax.media.jai.PlanarImage im;
	List<RenderedImage> images = new ArrayList<RenderedImage>();
	public TiffPrinter(String fileName) {
		// super();

		try {
			File file = new File(fileName);
			SeekableStream s = new FileSeekableStream(file);
			TIFFDecodeParam param = null;
			ImageDecoder dec = ImageCodec.createImageDecoder("tiff", s, param);
			System.out.println("Number of pages in this Tiff: " + dec.getNumPages());
			int count = dec.getNumPages();
			//int imageToLoad = 0;
			//im = new NullOpImage(dec.decodeAsRenderedImage(imageToLoad), null, OpImage.OP_IO_BOUND, null);
			for (int i = 1; i <= count; i++) {
				PlanarImage op = new NullOpImage(dec.decodeAsRenderedImage(i - 1), null, null, OpImage.OP_IO_BOUND);
				images.add(op.getAsBufferedImage());
			}

		} catch (Exception e) {
			System.out.println("Exception is " + e.getMessage());
		}
	}

	public int print(java.awt.Graphics g, java.awt.print.PageFormat pf, int pageIndex)
			throws java.awt.print.PrinterException {
		System.out.println(pageIndex);
		if (pageIndex >= images.size()) {
            return Printable.NO_SUCH_PAGE;
       }
		
		
		RenderedImage image = (RenderedImage) images.get(pageIndex);
		//if (pageIndex != 0)
		//	return NO_SUCH_PAGE;
		
		Graphics2D g2 = (Graphics2D) g;
		g2.translate(pf.getImageableX(), pf.getImageableY());

		int imgWidth = image.getWidth();
		int imgHeight = image.getHeight();
		double xRatio = (double) (pf.getImageableWidth() / imgWidth);
		double yRatio = (double) (pf.getImageableHeight() / imgHeight);
		//System.out.println("Image width: " + imgWidth + "\t Image height: " + imgWidth);
		//System.out.println("Page format width: " + pf.getImageableWidth() + "\t Page format height: "
		//		+ pf.getImageableHeight());
		//System.out.println("Image xRatio: " + xRatio + "\t Image yRatio: " + yRatio);

		g2.scale(xRatio, yRatio);

		AffineTransform at = AffineTransform.getTranslateInstance(pf.getImageableX(), pf.getImageableY());
		g2.drawRenderedImage(image, at);
		return PAGE_EXISTS;

	}

	public static void main(String args[]) throws Exception {
		//String fileName = args[0];
		String fileName = "new1.tif";
		final TiffPrinter myTif = new TiffPrinter(fileName);

		DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
		PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
		DocPrintJob job = printService.createPrintJob();
		PrintJobListener pjlistener = new PrintJobAdapter() {
			public void printDataTransferCompleted(PrintJobEvent e) {
				System.out.println("Good-bye");
				System.exit(0);
			}
		};

		PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
		pras.add(MediaSizeName.ISO_A4);
		// pras.add(new PrinterResolution(200,200,ResolutionSyntax.DPI));
		DocAttributeSet das = new HashDocAttributeSet();
		// das.add(new PrinterResolution(200,200,ResolutionSyntax.DPI));
		Doc doc = new SimpleDoc(myTif, flavor, das);
		try {
			job.print(doc, pras);
		} catch (PrintException pe) {
			pe.printStackTrace();
		}
	}

}
